Action Cableを試すサンプルアプリケーション
参考にする情報
Action Cable の概要 - Railsガイド
Rails 5: Action Cable demo - YouTube by DHH
パーフェクトRuby on Rails
試したいこと
基礎的なサブスクライブとブロードキャスト
1つのChannelに対して1つのStreamだけある場合の話
DHHの Rails 5: Action Cable demo - YouTube はそういう例
開いているページごとに、あるいはアクセスしているユーザごとに、異なるコンテンツをブロードキャストする
1つのChannelに対して複数のStreamがある場合の話
実装する機能
まずは匿名のMessageを作成/表示できるRoom
1つのChannelに対して1つのStreamだけある場合の話 のやつ
同一コンテンツをブロードキャスト
Rails 5: Action Cable demo - YouTubeに基づいて実装する
↑を拡張し、ログインしたUserのMessageも作成/表示できるようにする
ここでUser登録とログインをつくる
Messageにoptionalなuser_id(speaker_id)カラムを生やす
ログインしたUserがMessageを作成した場合、そのUserから見て「自身のMessageである」ことがわかるようにする
1つのChannelに対して複数のStreamがある場合の話
進捗
まずは匿名のMessageを作成/表示できるRoom
https://user-images.githubusercontent.com/26683960/111061477-f2748880-84e6-11eb-9ba0-541b1d43a2c7.gif
できた https://github.com/tanaken0515/chat-sample/pull/2
User登録とログインをつくる
つくった https://github.com/tanaken0515/chat-sample/pull/3
Messageにoptionalなuser_id(speaker_id)カラムを生やす
生やした https://github.com/tanaken0515/chat-sample/pull/4
ログインしたUserのMessageも作成/表示できるようにする
https://gyazo.com/cd7fb69042321b1c281be1c372371e08
できた https://github.com/tanaken0515/chat-sample/pull/5
User登録にバグがあったので修正した https://github.com/tanaken0515/chat-sample/pull/6
ログインしたUserがMessageを作成した場合、そのUserから見て「自身のMessageである」ことがわかるようにする
1つのChannelに対して複数のStreamがある場合の話
https://user-images.githubusercontent.com/26683960/111960416-119c9700-8b33-11eb-8fe2-dc181c725efc.gif
できた https://github.com/tanaken0515/chat-sample/pull/7
stream_for と broadcast_to をうまく使う必要がある